Overview:
-
An index makes database queries faster by avoiding of scanning the table from the beginning till the data is found.
-
A database index which is no longer required can be deleted through the SQL statement DROP INDEX.
-
In case a duplicate index has been created on to a table using Add Index option of the Alter Table SQL statement, it can be deleted through the SQL statement DROP INDEX. The SQL statement Create Index requires a name to be provided for the index being created. Hence creating a duplicate index is not possible with the SQL statement CREATE INDEX.
Example:
# Example Python program to delete a redundant index # MySQL client import # Create a connection to the MySQL Server dbSvrCon = pymysql.connect(host=dbServerIP, user=dbUsrName, password=dbUsrPwd, try: # SQL Statement - create table # Execute the sql statement # SQL Statement - create secondary index # Execute the sql - create secondary index # Redundant index is created # List indexes print(indexList) ########## Drop the redundant Index ########## # Print the index list except Exception as e: finally: |
Output:
[{'Table': 'Albums', 'Non_unique': 0, 'Key_name': 'PRIMARY', 'Seq_in_index': 1, 'Column_name': 'Id', 'Collation': 'A', 'Cardinality': 0, 'Sub_part': None, 'Packed': None, 'Null': '', 'Index_type': 'BTREE', 'Comment': '', 'Index_comment': '', 'Visible': 'YES', 'Expression': None}, {'Table': 'Albums', 'Non_unique': 1, 'Key_name': 'Album', 'Seq_in_index': 1, 'Column_name': 'Album', 'Collation': 'A', 'Cardinality': 0, 'Sub_part': None, 'Packed': None, 'Null': 'YES', 'Index_type': 'BTREE', 'Comment': '', 'Index_comment': '', 'Visible': 'YES', 'Expression': None}, {'Table': 'Albums', 'Non_unique': 1, 'Key_name': 'Album_2', 'Seq_in_index': 1, 'Column_name': 'Album', 'Collation': 'A', 'Cardinality': 0, 'Sub_part': None, 'Packed': None, 'Null': 'YES', 'Index_type': 'BTREE', 'Comment': '', 'Index_comment': '', 'Visible': 'YES', 'Expression': None}] ... ... ... |